include('manifest.lua')

require('hud')
require('round')
require('money')
require('props')
require('notification')

surface.CreateFont("ComickBook", 12, 400, true, false, "Flood12")
surface.CreateFont("ComickBook", 14, 400, true, false, "Flood14")
surface.CreateFont("ComickBook", 18, 400, true, false, "Flood18")
surface.CreateFont("ComickBook", 20, 400, true, false, "Flood20")
surface.CreateFont("ComickBook", 22, 400, true, false, "Flood22")
surface.CreateFont("ComickBook", 30, 400, true, false, "Flood30")
surface.CreateFont("ComickBook", 42, 400, true, false, "Flood42")
surface.CreateFont("ComickBook", 52, 400, true, false, "Flood52")

GM.PlayerIsConnected = false -- for my hacky playerisconnected work around.

function GM:Initialize()
	round.AddEvent(ROUND_BUILD, function() notification.AddLegacy("Build a boat! The Build Phase has begun!", NOTIFY_GENERIC, 4) end)
	round.AddEvent(ROUND_FIGHT, function()
		notification.AddLegacy("The Build Phase is over!", NOTIFY_GENERIC, 4)
		notification.AddLegacy("Fight to win!", NOTIFY_GENERIC, 4)
	end)
	round.AddEvent(ROUND_OVER, function()
		notification.AddLegacy("Congratulations Team 4CHAN won that round!", NOTIFY_GENERIC, 5)
		notification.AddLegacy("You gained $1000 this round. (+2% interest $1428)", NOTIFY_GENERIC, 5)
	end)
end

function GM:InitPostEntity()
	self.PlayerIsConnected = true
end

function GM:HUDPaint()
	hud.Draw()
end

function GM:Think()
	hud.Update()
end

-- Yes a little bit of a hacky work around, but it works.
usermessage.Hook("Flood_AddNotification", function(um)
	local text = um:ReadString()
	local nottype = um:ReadChar()
	local length = um:ReadChar()
	
	-- regex <3
	local function senditall()
		text = text:gsub("%[NAME%]", LocalPlayer():Name())
		local a = text:match("%[ROUND_([0-9])%]")
		if a then text = text:gsub("%[ROUND_%d%]", ROUNDSTRING[tonumber(a)]) end
		notification.AddLegacy(text, nottype, length)
	end
	
	if GAMEMODE.PlayerIsConnected then senditall() else timer.Simple(1, senditall) end
end)

local helpMenu = false

usermessage.Hook("OpenHelpMenu", function()
	if !helpMenu or !helpMenu:IsValid() then
		helpMenu = vgui.Create("HelpMenu")
	else
		helpMenu:Close()
		helpMenu = false
	end
end)